package com.rfid.assistant.activity.adapter;

import android.net.wifi.p2p.WifiP2pDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.assistant.R;
import com.rfid.assistant.bean.Machine;
import com.rfid.sockethelper.ClientManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UdpAdapter extends BaseAdapter<UdpAdapter.MyHolder> {

    private List<Machine> mMachines;
    public UdpAdapter(List<Machine> machines) {
        mMachines = machines;
    }

    @NonNull
    @Override
    public UdpAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_udp_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull UdpAdapter.MyHolder holder, int position) {
        Machine machine = mMachines.get(position);
        holder.ip.setText(machine.getIp() + "");
        holder.name.setText(machine.getName());
        holder.status.setText(getStatusStr(machine.getStatus()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onLongClick(position);
                }
                return false;
            }
        });
    }

    private String getStatusStr(int status) {
        if (status == ClientManager.STATUS_CONNECTED) {
            return "已连接";
        } else if (status == ClientManager.STATUS_CONNECTING) {
            return "正在连接";
        } else if (status == ClientManager.STATUS_NORMAL) {
            return "未连接";
        }
        return "未连接";
    }

    @Override
    public int getItemCount() {
        return mMachines.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.ip)
        TextView ip;
        @BindView(R.id.status)
        TextView status;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
